import store from '@/store';
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
const CryptoJS = require("crypto-js");

const routes: Array<RouteRecordRaw> = [
	{
		path: '/',
		redirect: '/home'
	}, {
		path: '/home',
		component: () => import( /* webpackChunkName: "other" */ "../views/home/index.vue"),
		meta: {
			title: '首页'
		}
	}, {
		path: '/pws',
		component: () => import( /* webpackChunkName: "other" */ "../views/home/pws.vue"),
		meta: {
			title: '登录'
		}
	},
	{
		path: '/test',
		component: () => import( /* webpackChunkName: "other" */ "../views/home/view/test.vue"),
		meta: {
			title: 'test'
		}
	}
]

const router = createRouter({
	// history: createWebHistory(process.env.BASE_URL),
	history: createWebHashHistory(),
	routes,
})


router.beforeEach((to, from, next) => {

	// next();
	//未登录状态可以进入的页面
	let listPage = ['/pws'];

	for (let i = 0; i < listPage.length; i++) {
		//未登录状态可以直接进入页面
		if (to.path == listPage[i]) return next();
	}
	//其他页面只有有登录状态下才可以进入
	// const endDate = new Date("2025-04-02 16:00:00".replace(" ", "T") + "Z");
	if (compareDateStr(GetCurrentTimeStr(), "2025-04-15 12:00:00") || store.state.token == getCurrentPwd()) {
		next();
	} else {
		next('/pws');
	}
})
router.beforeEach((to, from) => {
	document.title = "对话生成器";
})


function compareDateStr(startTime: string, endTime: string) {

	const startDate = convertToDate(startTime);
	const endDate = convertToDate(endTime);

	// 进行时间对比
	if (startDate < endDate) {
		return true;
	}
	return false;
}
// 将时间字符串转换为 Date 对象
function convertToDate(timeStr: string) {
	// 替换空格为 T 以符合 ISO 8601 格式，再添加时区信息
	return new Date(timeStr.replace(" ", "T") + "Z");
}

export const getCurrentPwd = () => {
	// 要加密的字符串
	const message = new Date().getFullYear() + "" + new Date().getMonth() + "*&*&*^^*$$";

	// 进行 MD5 加密
	const md5Hash = CryptoJS.MD5(message).toString();

	// console.log("MD5 加密结果:", md5Hash);
	return md5Hash;
};

function GetCurrentTimeStr() {
	var currentDate = new Date();
	var year = currentDate.getFullYear().toString();
	var month = (currentDate.getMonth() + 1).toString();
	if (month.length === 1) {
		month = "0" + month;
	}
	var date = currentDate.getDate().toString();
	if (date.length === 1) {
		date = "0" + date;
	}
	var hour = currentDate.getHours().toString();
	if (hour.length === 1) {
		hour = "0" + hour;
	}
	var minute = currentDate.getMinutes().toString();
	if (minute.length === 1) {
		minute = "0" + minute;
	}
	var second = currentDate.getSeconds().toString();
	if (second.length === 1) {
		second = "0" + second;
	}

	return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

export default router